package org.snail.web.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.log4j.Logger;
import org.snail.common.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * Created by WJ on 2017/12/8.
 */
@Configuration
//@ComponentScan({"org.snail"})
@PropertySource("classpath:context-datasource.properties")
@EnableTransactionManagement
public class DataSourceElConfig {

    private static final Logger LOGGER = Logger.getLogger(DataSourceElConfig.class);

    @Value("${workDesk.jdbc.driverclass}")
    private String driverClass;
    @Value("${workDesk.jdbc.url}")
    private String jdbcUrl;
    @Value("${workDesk.jdbc.username}")
    private String user;
    @Value("${workDesk.jdbc.password}")
    private String password;
    @Value("${workDesk.jdbc.poolsize.max}")
    private int maxPoolSize;
    @Value("${workDesk.jdbc.poolsize.min}")
    private int minPoolSize;
    @Value("${workDesk.jdbc.poolsize.initial}")
    private int initialPoolSize;
    @Value("${workDesk.jdbc.idletime.max}")
    private int maxIdleTime;
    @Value("${workDesk.jdbc.idleConnectionTestPeriod}")
    private int idleConnectionTestPeriod;

    @Value("${workDesk.read.jdbc.driverclass}")
    private String readDriverClass;
    @Value("${workDesk.read.jdbc.url}")
    private String readJdbcUrl;
    @Value("${workDesk.read.jdbc.username}")
    private String readUser;
    @Value("${workDesk.read.jdbc.password}")
    private String readPassword;
    @Value("${workDesk.read.jdbc.poolsize.max}")
    private int readMaxPoolSize;
    @Value("${workDesk.read.jdbc.poolsize.min}")
    private int readMinPoolSize;
    @Value("${workDesk.read.jdbc.poolsize.initial}")
    private int readInitialPoolSize;
    @Value("${workDesk.read.jdbc.idletime.max}")
    private int readMaxIdleTime;
    @Value("${workDesk.read.jdbc.idleConnectionTestPeriod}")
    private int readIdleConnectionTestPeriod;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    /**
     * MySql Master 写库数据源
     *
     * @return
     */
    @Bean(name = "platformTomcat", destroyMethod = "close")
    public DruidDataSource platformTomcat() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(jdbcUrl);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        dataSource.setMaxActive(maxPoolSize);
        dataSource.setInitialSize(initialPoolSize);

        return dataSource;
    }



    /**
     * 自动注入beanUtils
     * @return 返回beanUtils
     */
    @Bean("beanUtils")
    public BeanUtils getBeanUtils(){
        return new BeanUtils();
    }


    /**
     * MySql Slave 读库数据源
     *
     * @return
     */
    /*@Bean(name = "platformReadTomcat", destroyMethod = "close")
    public ComboPooledDataSource platformReadTomcat() {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setDriverClass(readDriverClass);
        }
        catch (PropertyVetoException e) {
            logger.error("数据源配置，无法找到配置类", e);
        }
        dataSource.setJdbcUrl(readJdbcUrl);
        dataSource.setUser(readUser);
        dataSource.setPassword(readPassword);
        dataSource.setMaxPoolSize(readMaxPoolSize);
        dataSource.setMinPoolSize(readMinPoolSize);
        dataSource.setInitialPoolSize(readInitialPoolSize);
        dataSource.setMaxIdleTime(readMaxIdleTime);
        dataSource.setTestConnectionOnCheckin(true);
        dataSource.setPreferredTestQuery("SELECT 1");
        dataSource.setIdleConnectionTestPeriod(readIdleConnectionTestPeriod);
        return dataSource;
    }
*/

}
